<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Contract;
use App\Models\Survey;

class Houses extends Model
{
    use SoftDeletes;
    protected $table='houses';
    protected $guarded=[];
    protected $dates = ['delete_at'];

    //定义房屋表和调查表关联关系
    public function survey(){
        return $this->belongsTo('App\Models\Survey');
    }
    //保存项目

    public static function newHouses($request){
        $data['project_id']=$request->project_id;
        $data['levy_house_num']=$request->levy_house_num;
        $data['owner']=$request->owner;
        $data['land_num']=$request->land_num;
        $data['house_property_num']=$request->house_property_num;
        $data['immovable_num']=$request->immovable_num;
        $data['is_mortgage']=$request->is_mortgage;
        $data['house_card_address']=$request->house_card_address;
        $data['ID_card']=$request->ID_card;
        $data['phone']=$request->phone;
        $data['ownership_type']=$request->ownership_type;
        $data['house_structure']=$request->house_structure;
        $data['house_level']=$request->house_level;
        $data['house_nature']=$request->house_nature;
        $data['house_use']=$request->house_use;
        $data['built_year']=$request->built_year;
        $data['built_area']=$request->built_area;
        $data['house_card_area']=$request->house_card_area;
        /*$data['remove_money']=$request->remove_money;
        $data['compensation_money']=$request->compensation_money;
        $data['service_fee']=$request->service_fee;
        $data['pay_money']=$request->pay_money;*/
        $data['remark']=$request->remark;
        $newHouses=Houses::create($data);
        if($newHouses){
            return redirect()->back()->with('status','小主！新建成功！');
        }else{
            return redirect()->back()->with('status','小主！新建失败!');
        }
    }

    /*
    * 修改项目
    * */
    public static function updateHouses($request){
        $data['project_id']=$request->project_id;
        $data['levy_house_num']=$request->levy_house_num;
        $data['owner']=$request->owner;
        $data['land_num']=$request->land_num;
        $data['house_property_num']=$request->house_property_num;
        $data['immovable_num']=$request->immovable_num;
        $data['is_mortgage']=$request->is_mortgage;
        $data['house_card_address']=$request->house_card_address;
        $data['ID_card']=$request->ID_card;
        $data['phone']=$request->phone;
        $data['ownership_type']=$request->ownership_type;
        $data['house_structure']=$request->house_structure;
        $data['house_level']=$request->house_level;
        $data['house_nature']=$request->house_nature;
        $data['house_use']=$request->house_use;
        $data['built_year']=$request->built_year;
        $data['built_area']=$request->built_area;
        $data['house_card_area']=$request->house_card_area;
        /*$data['remove_money']=$request->remove_money;
        $data['compensation_money']=$request->compensation_money;
        $data['service_fee']=$request->service_fee;
        $data['pay_money']=$request->pay_money;*/
        $data['remark']=$request->remark;
        $updateHouses=Houses::where('id',$request->houses_id)->update($data);
        if($updateHouses){
            return  back()->with('status','修改成功');
        }else{
            return  back()->with('status','项目信息未做任何修改');
        }
    }

    /*
     * 发送至签订合同
     * */
    public static function sendToContract($request)
    {
        $houses = Houses::where('id',$request->housesId)->first();
        $check=Contract::where('project_id',$houses['project_id'])->where('house_id',$houses['house_id'])->first();
        $data['project_id']=$houses['project_id'];
        $data['house_id']=$request->housesId;
        
        Contract::create($data);
        Houses::where('id',$request->housesId)->update(['is_send_contract'=>1]);
        $backMsg=['msg'=>200];
        return json_encode($backMsg);



    }
}